﻿using System.Threading;
using System.Web;
using System.Web.Mvc;
using Microsoft.Owin.Security;

namespace Inovout.Membership.Account.WebPage.Controllers
{
    public class OAuthdController : Controller
    {
        [AllowAnonymous]
        public ActionResult Authorize(string id,string authType)
        {
            authType = authType ?? id ?? "Forms";
            if (Thread.CurrentPrincipal.Identity.IsAuthenticated)
            {
                return Redirect(Url.Action("Authorize", "OAuth") + this.Request.Url.Query);
            }
            else
            {
                //根据privoder跳转至ExtensonalSignIn或SignIn
                var authentication = HttpContext.GetOwinContext().Authentication;
                if (authType.ToLower() == "mail" || authType.ToLower() == "dynamicpassword" || authType.ToLower() == "forms")
                {
                    authentication.Challenge(authType);
                    return new HttpUnauthorizedResult();
                }

                var properties = new AuthenticationProperties()
                {
                    RedirectUri = Url.Action("Authorize", "OAuth") + this.Request.Url.Query
                };
                authentication.Challenge(properties, authType);
                return new HttpUnauthorizedResult();
            }
        }
    }

}
